package com.ccx.library.module.menu.controller;

import com.ccx.library.module.menu.entity.Menu;
import com.ccx.library.module.menu.service.MenuService;
import com.ccx.library.response.R;
import com.ccx.library.util.MenuUtil;
import com.ccx.library.util.ModelVoIdUtil;
import com.ccx.library.util.WrapperUtil;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@Api(tags = "menu")
@RequestMapping("/menu")
public class MenuController {
    @Autowired
    private MenuService menuService;
    @Autowired
    private MenuUtil menuUtil;
    @GetMapping("/query")
    public R Query(Menu menu){
        return R.SUCCESS(menuService.getOne(WrapperUtil.Query(menu)));
    }
    @GetMapping
    public R list(){
        return R.SUCCESS(menuUtil.DynamicsMenusProcess(menuService.list()));
    }
    private final static Integer ROOT_NODE = 0;
    @GetMapping("/queryRootNode")
    public R queryRootNode(){
        List<Menu> menuList = menuService.list();
        List<Menu> RootList = new ArrayList<>();
        menuList.forEach(menu -> {
            if(ROOT_NODE.equals(menu.getType())){
                RootList.add(menu);
            }
        });
        return R.SUCCESS(RootList);
    }
    @PostMapping
    public R save(@RequestBody Menu menu){
        return R.SUCCESS(menuService.save(menu));
    }
    @PutMapping
    public R edit(@RequestBody Menu menu){
        return R.SUCCESS(menuService.update(WrapperUtil.Update(menu, ModelVoIdUtil.getId(menu),menu.getMenu_id())));
    }
}
